package com.stylefeng.guns.modular.bus.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.*;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.service.IDeptService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.BusPrdTrace;
import com.stylefeng.guns.modular.bus.service.IBusPrdTraceService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * prdTrace控制器
 *
 * @author fengshuonan
 * @Date 2018-04-21 18:00:23
 */
@Controller
@RequestMapping("/busPrdTrace")
public class BusPrdTraceController extends BaseController {

    private String PREFIX = "/bus/busPrdTrace/";

    @Autowired
    private IBusPrdTraceService busPrdTraceService;
    @Autowired
    private IDeptService deptService;
    /**
     * 跳转到prdTrace首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "busPrdTrace.html";
    }

    /**
     * 跳转到添加prdTrace
     */
    @RequestMapping("/busPrdTrace_add")
    public String busPrdTraceAdd() {
        return PREFIX + "busPrdTrace_add.html";
    }

    /**
     * 跳转到修改prdTrace
     */
    @RequestMapping("/busPrdTrace_update/{busPrdTraceId}")
    public String busPrdTraceUpdate(@PathVariable Integer busPrdTraceId, Model model) {
        BusPrdTrace busPrdTrace = busPrdTraceService.selectById(busPrdTraceId);
        model.addAttribute("item",busPrdTrace);
        LogObjectHolder.me().set(busPrdTrace);
        return PREFIX + "busPrdTrace_edit.html";
    }

    /**
     * 获取prdTrace列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Map map) {
        Integer comId=ShiroKit.getUser().getDeptId();
        Dept dept=deptService.selectById(comId);
        Integer comPid=dept.getPid();
        KaptchaUtil.comAuth(map,comPid,String.valueOf(comId));
        List<BusPrdTrace> busPrdTraceList=busPrdTraceService.selectTraceBatchByMap(map);
        for (BusPrdTrace busPrdTrace:busPrdTraceList) {
            busPrdTrace.setQrCodeFileUrl(OSSUtil.getFileUrl(busPrdTrace.getQrCodeFileName(),Config.expires));
        }
        return busPrdTraceList;
    }

    /**
     * 新增prdTrace
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseData add(BusPrdTrace busPrdTrace) {
        Map map=new HashMap();
        busPrdTrace.setPrdTraceMaximum(Integer.parseInt(busPrdTrace.getPrdTraceMinimum())+busPrdTrace.getPrdTraceBatchcount()-1+"");
        map.put("prdId",busPrdTrace.getPrdId());
        //map.put("comId",busPrdTrace.getComId());
        map.put("prdSellArea",busPrdTrace.getPrdSellArea());
        map.put("prdTraceMinimum",busPrdTrace.getPrdTraceMinimum());
        map.put("prdTraceMaximum",busPrdTrace.getPrdTraceMaximum());
        List list=busPrdTraceService.selectTraceBatchByMap(map);
        if(list.size()==0)
        {
            busPrdTrace.setDatadate(KaptchaUtil.getNowDate8());
            busPrdTrace.setPrdSellArea(KaptchaUtil.getRandomString(Config.sellAreaLength));
            //busPrdTraceService.insert(busPrdTrace);
            busPrdTrace.setId(KaptchaUtil.getUUID());
            busPrdTraceService.createBatchQrcodeFile(busPrdTrace);
            busPrdTraceService.insertTrace(busPrdTrace);
        }else
        {
            return new ResponseData(null, ServiceStatus.CREATED);
        }

        return new ResponseData(null,ServiceStatus.OK);
    }

    /**
     * 删除prdTrace
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String busPrdTraceId) {
        busPrdTraceService.deleteById(busPrdTraceId);
        return SUCCESS_TIP;
    }

    /**
     * 修改prdTrace
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BusPrdTrace busPrdTrace) {
        busPrdTraceService.updateById(busPrdTrace);
        return SUCCESS_TIP;
    }

    /**
     * prdTrace详情
     */
    @RequestMapping(value = "/detail/{busPrdTraceId}")
    @ResponseBody
    public Object detail(@PathVariable("busPrdTraceId") Integer busPrdTraceId) {
        return busPrdTraceService.selectById(busPrdTraceId);
    }
    @RequestMapping("/download")
    public ResponseEntity<byte[]>   download(@RequestParam("qrCodeFileName") String qrCodeFileName,HttpServletRequest request,HttpServletResponse response) throws IOException {
       /* try {
            // 设置返回的文件编码
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition", "attchement;filename=" + qrCodeFileName);
            HttpStatus statusCode = HttpStatus.OK;
            ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(IOUtils.toByteArray(OSSUtil.downloadFile(qrCodeFileName)), headers, statusCode);
            return  entity;
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        HttpHeaders headers = new HttpHeaders();
        String fileName=new String(qrCodeFileName.getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(IOUtils.toByteArray(OSSUtil.downloadFile(qrCodeFileName)),
                headers, HttpStatus.CREATED);
    }

}
